package com.wistech.search.solr.extend;

import java.util.Map;

import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.valuesource.SimpleFloatFunction;

import com.wistech.commons.utils.spring.SpringContextHolder;

/**
 * 销量函数，用于产品按销量排序
 * 
 * @author Liuye
 * 
 */
public class SalesFunction extends SimpleFloatFunction {
	private Map<String, Float> orderNums;

	public SalesFunction(ValueSource source) {
		super(source);
		ExternalDataContext context = SpringContextHolder
				.getBean("externalDataContext");
		orderNums = context.getProductOrderNums();
	}

	@Override
	protected float func(int doc, FunctionValues vals) {
		String id = vals.strVal(doc);
		Float sales = orderNums.get(id);
		// System.out.println("doc:" + doc + " id:" + id + " sales:" + sales);
		return sales == null ? 0 : sales;
	}

	@Override
	protected String name() {
		return "sales";
	}

}
